home *** CD-ROM | disk | FTP | other *** search
/ Freelog 117 / FreelogNo117-OctobreNovembre2013.iso / Programmation / jedit / jedit5.1.0install.exe / {app} / macros / Java / Get_Package_Name.bsh < prev    next >
Text File  |  2013-07-28  |  4KB  |  143 lines

  1. /*
  2.  * Get_Package_Name.bsh - a BeanShell macro script for the
  3.  * jEdit text editor - insert package name based upon path
  4.  * of current buffer
  5.  * Copyright (C) 2001 John Gellene
  6.  * jgellene@nyc.rr.com
  7.  * http://community.jedit.org
  8.  *
  9.  * This program is free software; you can redistribute it and/or
  10.  * modify it under the terms of the GNU General Public License
  11.  * as published by the Free Software Foundation; either version 2
  12.  * of the License, or any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  22.  *
  23.  * Based on code contributed by Richard Wan
  24.  *
  25.  * $Id: Get_Package_Name.bsh 21381 2012-03-15 20:02:15Z jojaba_67 $
  26.  *
  27.  * Checked for jEdit 4.0 API
  28.  *
  29.  */
  30.  
  31.  
  32. //Localization
  33. final static String NotWorking1Error = jEdit.getProperty("macro.rs.GetPackageName.NotWorking1.error", "This macro will not work when the Java interpreter");
  34. final static String NotWorking2Error = jEdit.getProperty("macro.rs.GetPackageName.NotWorking2.error", "loads jEdit with the '-jar' command line option.");
  35. final static String NotFindError = jEdit.getProperty("macro.rs.GetPackageName.NotFind.error", "Could not find a package name."); 
  36. final static String NotEditableMessage = jEdit.getProperty("macro.rs.general.ErrorNotEditableDialog.message", "Buffer is not editable");
  37.  
  38. boolean testClassPath()
  39. {
  40.     classpath = System.getProperty("java.class.path");
  41.     classSeparator = (File.separatorChar == '/' ? ':' : ';');
  42.     return (classpath.indexOf(classSeparator) != -1)
  43.         || (!classpath.endsWith("jedit.jar"));
  44. }
  45.  
  46.  
  47. File getCanonicalFile(File file)
  48. {
  49.     try
  50.     {
  51.         return new File(file.getCanonicalPath());
  52.     }
  53.     catch(IOException e)
  54.     {
  55.         return new File(file.getAbsolutePath());
  56.     }
  57. }
  58.  
  59. File getRoot(File file)
  60. {
  61.     tokens = new StringTokenizer(System.getProperty("java.class.path"),
  62.         File.pathSeparator);
  63.     fileSet = new Hashtable();
  64.     while(tokens.hasMoreTokens())
  65.     {
  66.         String tok = tokens.nextToken();
  67.         fileSet.put(getCanonicalFile(new File(tok)), tok);
  68.     }
  69.  
  70.     while(file != null)
  71.     {
  72.         if(fileSet.get(getCanonicalFile(file)) != null)
  73.             break;
  74.         parent = file.getParent();
  75.         file = (parent != null) ? new File(parent) : null;
  76.     }
  77.     return file;
  78. }
  79.  
  80.  
  81. String determinePackageName(String path)
  82. {
  83.     pathFile = new File(buffer.getPath());
  84.     File root = getRoot(pathFile);
  85.     if(root == null)
  86.         return null;
  87.     parent = pathFile.getParent();
  88.     packagePath = parent.substring(root.getPath().length(), parent.length());
  89.     packagePath = packagePath.replace(File.separatorChar, '.');
  90.     if (packagePath.endsWith("."))
  91.     {
  92.         packagePath = packagePath.substring(0, packagePath.length() - 1);
  93.     }
  94.     if (packagePath.startsWith("."))
  95.     {
  96.         packagePath = packagePath.substring(1, packagePath.length());
  97.     }
  98.     return packagePath;
  99. }
  100.  
  101. if( buffer.isReadOnly() )
  102.     Macros.error( view, NotEditableMessage );
  103. else
  104. {
  105.     // main routine
  106.     if(!testClassPath())
  107.     {
  108.         Macros.error(view, NotWorking1Error + "\n" + NotWorking2Error);
  109.     }
  110.     else
  111.     {
  112.         result = determinePackageName(buffer.getPath());
  113.         if(result == null)
  114.         {
  115.             Macros.error(view, NotFindError);
  116.         }
  117.         else textArea.setSelectedText(result);
  118.     }
  119. }
  120.  
  121. /*
  122.     Macro index data (in DocBook format)
  123.  
  124. <listitem>
  125.     <para><filename>Get_Package_Name.bsh</filename></para>
  126.     <abstract><para>
  127.         Inserts a plausible Java package name for the current buffer.
  128.     </para></abstract>
  129.     <para>
  130.         The macro compares the buffer's path name with the elements of the
  131.         classpath being used by the jEdit session.  An error message will be
  132.         displayed if no suitable package name is found. This macro will not
  133.         work if jEdit is being run as a JAR file without specifying a
  134.         classpath.  In that case the classpath seen by the macro consists
  135.         solely of the JAR file.
  136.     </para>
  137. </listitem>
  138.  
  139. */
  140.  
  141. // end Get_Package_Name.bsh
  142.  
  143.